function giveLable() {
  $('.niurenbox').each(function (index, ele) {
    var labelStr = $(ele).find('.labelTitle').val(),
        labelArray = labelStr.split('---');
    var str1 = $(ele).find('.fiveday').val() || '',
        str2 = $(ele).find('.nextday').val() || '',
        str3 = $(ele).find('.threeday').val() || '',
        str4 = $(ele).find('.risen').val() || '',
        titleArr = ['.crpjzf', '.ggzdzf'];
    labelArray.forEach(function (value, num) {
      if (num == 5) {
        $(ele).find('.left').addClass('hasHonor').append($('<p class="honor">' + value + '</p>'));
      } else {

      }
    });
    if (str1.indexOf('---') > 0 && titleArr.length > 0) {
      main1(str1, titleArr, ele);
    }
    if (str2.indexOf('---') > 0 && titleArr.length > 0) {
      main2(str2, titleArr, ele);
    }
    if (str3.indexOf('---') > 0 && titleArr.length > 0) {
      main3(str3, titleArr, ele);
    }
    if (str4.indexOf('---') > 0 && titleArr.length > 0) {
      main4(str4, titleArr, ele);
    }
  })
}

function main1(str, arr, ele) {
  var dataArray = str.split('---');
  var data = [];
  var spans = $(ele).find(arr.shift()).find('span');
  dataArray.forEach(function (value) {
    var value = value.split('+');
    data.push(value[1]);
  });
  spans.eq(0).html('五日平均涨幅');
  spans.eq(1).html(function () {
    var totle = data.reduce(function (pre, cur) {
      return Number(pre) + Number(cur)
    });
    var average = parseInt(totle / data.length * 100) / 100;
    return average + '%';
  });
}
function main2(str, arr, ele) {
  var dataArray = str.split('---');
  var title = dataArray.pop();
  var spans = $(ele).find(arr.shift()).find('span');
  spans.eq(0).html(title);
  spans.eq(1).html(dataArray[0])
}
function main3(str, arr, ele) {
  var dataArray = str.split('---');
  var title = dataArray.pop();
  var data = [];
  var spans = $(ele).find(arr.shift()).find('span');
  dataArray.forEach(function (value) {
    value = value.split('+');
    data.push(Math.max.call(undefined, Number(value[1]), Number(value[2])));
  });
  spans.eq(0).html(title);
  spans.eq(1).html(Math.max.apply(undefined, data) + '%')
}
function main4(str, arr, ele) {
  var dataArray = str.split('---');
  var title = dataArray.pop();
  var spans = $(ele).find(arr.shift()).find('span');
  var data = [], totalRise, risePercent;
  dataArray.forEach(function (value) {
    value = value.split('+');
    data.push(value[1]);
  });
  totalRise = data.reduce(function (previousValue, currentValue) {
    currentValue = currentValue < 1 ? 0 : currentValue;
    return Number(previousValue) + Number(currentValue);
  }, 0);
  risePercent = parseInt(totalRise * 100 / (data.length * 2)) + '%';
  spans.eq(0).html(title);
  spans.eq(1).html(risePercent);
}

giveLable();


var baseUrl = '/txsecurities-all/wximages/vdoubleRd2/';
// var baseUrl = '../wximages/vdoubleRd2/';
var images = ['junyinniuren.jpg', 'junyinniuren1.jpg', 'junyinniuren2.jpg'];
function carousel() {
  var imgBox = [];
  var carouselClass = 'carousel';
  var carousel = $('<div class="' + carouselClass + '"></div>');
  var me = $(this);
  var boxWidth = me.width();
  var proportion = 226 / 640;
  me.height(parseInt(boxWidth * proportion));
  images.forEach(function (val, index) {
    //imgBox.push($('<div><a style="display:block;width:100%;height:100%;" href="http://weixin.cjtglm.com/txsecurities-all/wx/toDialList/start/"><img src="' + baseUrl + val + '" alt=""></a></div>'));
    imgBox.push($('<div><img src="' + baseUrl + val + '" alt=""></div>'));
    carousel.append(imgBox[index]);
  });
  me.append(carousel);

  me.Swipe({
    startSlide: 2,
    speed: 400,
    auto: 3000,
    continuous: true,
    disableScroll: false,
    stopPropagation: false,
    callback: function (index, elem) {
    },
    transitionEnd: function (index, elem) {
    }
  })
}


var winMans = [
  {
    name: '龙隐',
    title: '涨停王',
    image: 'longyin.jpg',
    down1: ['湖南天雁', '2017.01.04', '5日最高涨幅', '41.94%', '#fe7f55'],
    down2: ['海螺型材', '2017.01.13', '4日最高涨幅', '11.14%', '#fc5e55']
  }
  , {
    name: '苏学鹏',
    title: '改革牛人',
    image: 'suxuepeng.jpg',
    down1: ['两面针', '2017.01.05', '3日最高涨幅', '19.56%', '#8a79e2'],
    down2: ['中兵红箭', '2017.01.06', '3日最高涨幅', '16.70%', '#ffcc00']
  }
  , {
    name: '左易',
    title: '波段达人',
    image: 'zuoyi.png',
    down1: ['华安证券', '2017.02.09', '6日最高涨幅', '18.56%', '#ff9600'],
    down2: ['百联股份', '2017.02.13', '5日最高涨幅', '16.92%', '#2ea6f5']
  }
  /*    ,{
   name:'吕向召',
   title:'军工专家',
   image:'lvxiangzhao.jpg',
   down1:['光电股份','2017.01.06','2日最高涨幅','17.04%','#ff9600'],
   down2:['北方导航','2017.01.06','3日最高涨幅','13.48%','#2ea6f5']
   }*/
  /*    ,{
   name:'周康',
   title:'短线擒牛',
   image:'zhoukang.jpg',
   down1:['华灿光电','2017.01.16','3日最高涨幅','21.47%','#ff9600'],
   down2:['大禹节水','2017.01.16','次日最高涨幅','7.81%','#2ea6f5']
   }*/
  /*    ,{
   name:'黄家诚',
   title:'稳中见涨',
   image:'huangjiacheng.jpg',
   down1:['润邦股份','2017.01.13','2日最高涨幅','8.45%','#8a79e2'],
   down2:['陕西煤业','2017.01.16','3日最高涨幅','6.15%','#ffcc00']
   }*/
];

var texts = [
  /*    '<span>2017.01.04推出<b>湖南天雁</b>5日最高涨幅<b>41.94%</b></span>',
   '<span>2017.01.05推出<b>珠江啤酒</b>2日最高涨幅<b>11.92%</b></span>'*/
];
function getTexts(arr) {
  var str = '<span>' + arr[1].slice(5) + '推出<b>' + arr[0] + '</b>' + arr[2] + '<b>' + arr[3] + '</b></span>';
  texts.push(str);
}

winMans.forEach(function (datas) {
  getTexts(datas.down1);
  getTexts(datas.down2);
});


function notice() {
  var txtBox = [];
  var carouselClass = 'carousel';
  var carousel = $('<div class="' + carouselClass + '"></div>');
  var me = $(this);
  var boxWidth = me.width();
  carousel.width(boxWidth * (texts.length + 1));
  texts.forEach(function (val, index) {
    txtBox.push($(val).width(boxWidth));
    carousel.append(txtBox[index]);
  });
  txtBox.push($(texts[0]).width(boxWidth));
  carousel.append(txtBox[txtBox.length - 1]);
  me.append(carousel);


  function moving(ele) {
    var index = ele.data('index') || 0;
    var width = ele.children().width();
    var length = ele.children().length;
    if (++index < length) {
      if (index == 1) {
        ele.css({
          'transition': 'margin 5s linear',
          'webkitTransition': 'margin 5s linear'
        });
      }
      ele.data('index', index);
      ele.css({
        'marginLeft': -width * index
      });
      setTimeout(moving, 6000, carousel)
    } else {
      index = 0;
      ele.css({
        'transition': '',
        'webkitTransition': ''
      });
      ele.data('index', index);
      ele.css({
        'marginLeft': -width * index
      });
      setTimeout(moving, 1000, carousel);
    }
  }

  //启动Moving
  setTimeout(moving, 1000, carousel);
}


function winUp(datas) {
  var upMan = $('.niurenbox:contains(' + datas.name + ')');
  var link = upMan.attr('href');
  console.log(link);
  var upManBox =
      '<a href="' + link + '" class="niurenboxWin">'
      + '<div class="left one">'
      + '<img src="' + baseUrl + datas.image + '"/>'
      + '</div>'
      + '<div class="left two">'
      + '<div class="up">'
      + '<span class="up1">' + datas.name + '</span>'
      + '<span class="up2">' + datas.title + '</span>'
      + '</div>'
      + '<div class="down">'
      + '<div class="down1" style="box-shadow: 4px 4px 10px 0 ' + datas.down1[4] + ';background: ' + datas.down1[4] + ';">'
      + '<span class="stock">' + datas.down1[0] + '</span>'
      + '<span class="time">' + datas.down1[1] + '</span>'
      + '<span class="words">' + datas.down1[2] + '</span>'
      + '<span class="result">' + datas.down1[3] + '</span>'
      + '</div>'
      + '<div class="down2" style="box-shadow: 4px 4px 10px 0 ' + datas.down2[4] + ';background: ' + datas.down2[4] + ';">'
      + '<span class="stock">' + datas.down2[0] + '</span>'
      + '<span class="time">' + datas.down2[1] + '</span>'
      + '<span class="words">' + datas.down2[2] + '</span>'
      + '<span class="result">' + datas.down2[3] + '</span>'
      + '</div>'
      + '</div>'
      + '</div>'
      + '<span class="link">查看详情 ></span>'
      + '</a>';
  $('.niurenboxesWin').append(upManBox);
  upMan.hide();

}
winMans.forEach(winUp);


jQuery.fn.extend({
  carousel: carousel,
  notice: notice
});

$(function () {
  $('.bannerZxt').carousel();
  $('.noticeZxt').notice();
});


